package 代码记录.指定公司代码练习记录.华为_2024.分值100;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author zx
 * @create 2024-02-02 20:10
 * 人家的思路，使用List保存树的子节点；看懂了，应该可以自己做出来
 */
public class 最富裕的小家庭的财富和_34 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] arr = new int[N + 1];
        for (int i = 1; i <= N; i++) {
            arr[i] = sc.nextInt();
        }
        Map<Integer, ArrayList<Integer>> mp = new HashMap<>();
        for (int i = 0; i < N - 1; i++) {
            int x1 = sc.nextInt();
            int x2 = sc.nextInt();
            if (mp.containsKey(x1)) {
                mp.get(x1).add(x2);
            } else {
                ArrayList<Integer> list = new ArrayList<>();
                list.add(x2);
                mp.put(x1, list);
            }
        }
        int res = 0;
        for (Map.Entry<Integer, ArrayList<Integer>> entry : mp.entrySet()) {
            int key = entry.getKey();
            int cur = arr[key];
            for (int i : entry.getValue()) {
                cur += arr[i];
            }
            res = Math.max(res, cur);
        }
        System.out.println(res);
    }
}
